﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataAccess {
    public class EndUpdateInventoryRepository : DataProvider {
        public DataTable getAll(int partnerID) {
            /*string sql = "SELECT EndUpdateInventoryLog.ID, EndUpdateInventoryLog.Trans_ID, EndUpdateInventoryLog.Msg, Transactions.ID AS TransactionID, Partners.PartnerLoginName, LoginLog.SID, LoginLog.Time " +
                         "FROM EndUpdateInventoryLog INNER JOIN " +
                         " LoginLog ON EndUpdateInventoryLog.SID = LoginLog.SID INNER JOIN " +
                         " Partners ON LoginLog.PartnerID = Partners.PartnerID INNER JOIN " +
                         " Transactions ON EndUpdateInventoryLog.Trans_ID = Transactions.ID AND LoginLog.SID = Transactions.SID " +
                         " WHERE (Partners.PartnerID = {0})";*/
            string sql = "SELECT EndUpdateInventoryLog.Msg, Transactions.ID  AS TransactionID, Transactions.TransID, LoginLog.SID, LoginLog.Time, Partners.PartnerLoginName  " +
                         "FROM Transactions INNER JOIN " +
                         "EndUpdateInventoryLog ON Transactions.TransID = EndUpdateInventoryLog.Trans_ID INNER JOIN " +
                         "LoginLog ON EndUpdateInventoryLog.SID = LoginLog.SID INNER JOIN " +
                         "Partners ON LoginLog.PartnerID = Partners.PartnerID " +
                         " WHERE (Partners.PartnerID = {0}) ";
            sql = string.Format(sql, partnerID);
            return this.executeQueryDataTable(sql);
        }

        public bool add(String SID, string trans_id, String Msg) {
            String sql = "INSERT INTO EndUpdateInventoryLog(SID, Trans_ID, Msg) VALUES('{0}', '{1}', '{2}')";
            sql = string.Format(sql, SID, trans_id, Msg);

            if(this.executeQuery(sql) > 0)
                return true;

            return false;
        }

        public int getTranID(String SID, String TransID) {
            String sql = "SELECT Transactions.ID " +
                         "FROM LoginLog, Transactions " +
                         "WHERE (LoginLog.SID = '{0}') AND (Transactions.TransID = '{1}')";
            sql = string.Format(sql, SID, TransID);
            int transID = 0;
            try {
                transID = int.Parse(this.executeScalar(sql).ToString());
            }
            catch (Exception ex) {
            }
            return transID;
        }
    }
}
